package com.company.news;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.cheng.channel.Channel;
import com.cheng.channel.ChannelView;
import com.company.news.common.MyActivity;
import com.company.news.constants.Constant;
import com.company.news.other.RefreshEvent;
import com.company.news.utils.PreUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yun.common.utils.SharePreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Lovelin on 2019/5/31
 * <p>
 * Describe:
 */
public class ChannelSelectedActivity extends MyActivity implements ChannelView.OnChannelListener {
    private String TAG = getClass().getSimpleName();
    @BindView(R.id.channelView)
    ChannelView channelView;
    private Gson mGson = new Gson();
    private List<Channel> finishChannelList;
    private List<com.company.news.bean.Channel> mSaveChannelList;
    private List<com.company.news.bean.Channel> mSaveRecommendChannelList1;
    private List<List<Channel>> otherChannelList;
    private List<Channel> myChannelList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_channels_selected;
    }

    @Override
    protected int getTitleId() {
        return 0;
    }

    @Override
    protected void initView() {
        String selectedChannelJson = PreUtils.getString(Constant.SELECTED_CHANNEL_JSON, "");
        String unselectChannel = PreUtils.getString(Constant.UNSELECTED_CHANNEL_JSON, "");

        List<com.company.news.bean.Channel> selectedChannel = mGson.fromJson(selectedChannelJson, new TypeToken<List<com.company.news.bean.Channel>>() {
        }.getType());
        List<com.company.news.bean.Channel> unselectedChannel = mGson.fromJson(unselectChannel, new TypeToken<List<com.company.news.bean.Channel>>() {
        }.getType());
//        本地没有title
//        String[] channels = getResources().getStringArray(R.array.channel);
//        String[] channelCodes = getResources().getStringArray(R.array.channel_code);
/**
 * 先获取全部选中和未选中的channels  在计算出 未选中的channels      注意,切换channels的bean类型
 */

//转换城选择器的channel

        myChannelList = new ArrayList<>();
        List<Channel> recommendChannelList1 = new ArrayList<>();

        for (int i = 0; i < selectedChannel.size(); i++) {
            String aMyChannel = selectedChannel.get(i).title;
            Channel channel;
            //名字 ,未知,角标
            channel = new Channel(aMyChannel, 1, i);
            myChannelList.add(channel);
        }
        if (unselectedChannel != null) {
            for (int i = 0; i < unselectedChannel.size(); i++) {
                Channel channel = new Channel(unselectedChannel.get(i).title, 1, i);
                recommendChannelList1.add(channel);
            }

        }
        channelView.setChannelFixedCount(3);
        channelView.addPlate("我的频道", myChannelList);
        channelView.addPlate("推荐频道", recommendChannelList1);
//        channelView.addPlate("国内", recommendChannelList2);
//        channelView.addPlate("国外", recommendChannelList3);
        channelView.inflateData();
        channelView.setChannelNormalBackground(R.drawable.bg_channel_normal);
        channelView.setChannelEditBackground(R.drawable.bg_channel_edit);
        channelView.setChannelFocusedBackground(R.drawable.bg_channel_focused);
        channelView.setOnChannelItemClickListener(this);

    }


    @Override
    protected void initData() {

    }


    @Override
    public boolean isStatusBarEnabled() {
        return !super.isStatusBarEnabled();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void channelItemClick(int position, Channel channel) {
        String channelName = channel.getChannelName();
        if (isClickFinish){
            for (int i = 0; i < mSaveChannelList.size(); i++) {
                String title = mSaveChannelList.get(i).title;
                if (channelName.equals(title)) {
                    String channelCode = mSaveChannelList.get(i).channelCode+"";
                    SharePreferenceUtil.put(this, SharePreferenceUtil.Tab_Selected_Index, channelCode);
                }
            }
        }else{
            for (int i = 0; i < myChannelList.size(); i++) {
                String title = myChannelList.get(i).getChannelName();
                if (channelName.equals(title)) {
                    Channel channel1 = myChannelList.get(i);

                    String obj = myChannelList.get(i).getObj()+"";
                    Log.e(TAG, "ItemClick===="  + ".===Object=i====." + obj);

                    SharePreferenceUtil.put(this, SharePreferenceUtil.Tab_Selected_Index,obj);
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        List<Channel> channelList = channelView.getMyChannel();
        otherChannelList = channelView.getOtherChannel();
        sendRefreshEvent(channelList);
        finish();
    }

    private void sendRefreshEvent(List<Channel> channelList) {
        changeChannels(channelList);
        PreUtils.putString(Constant.SELECTED_CHANNEL_JSON, mGson.toJson(mSaveChannelList));
        //allList  此时删除了选中的channel
        PreUtils.putString(Constant.UNSELECTED_CHANNEL_JSON, mGson.toJson(mSaveRecommendChannelList1));
        EventBus.getDefault().post(new RefreshEvent());
    }

    @Override
    public void channelEditFinish(List<Channel> channelList) {
        sendRefreshEvent(channelList);
    }

    private boolean isClickFinish = false;

    private void changeChannels(List<Channel> channelList) {
        isClickFinish =true;
        otherChannelList = channelView.getOtherChannel();
        finishChannelList = new ArrayList<>();
        //此时我们要存原来的channel 类型
        mSaveChannelList = new ArrayList<>();
        mSaveRecommendChannelList1 = new ArrayList<>();
        mSaveChannelList.clear();
        for (int i = 0; i < channelList.size(); i++) {
            Channel channel1 = channelList.get(i);
            com.company.news.bean.Channel channel = new com.company.news.bean.Channel(channel1.getChannelName(), i + "");
            mSaveChannelList.add(channel);
            String title = mSaveChannelList.get(i).title;
            String channelCode = mSaveChannelList.get(i).channelCode;
//            Log.e("ChannelSelectedActivity", "====-选中的--==频道名字==" + title + "=====code=====" + channelCode);

        }

        //获取到未选中的频道   这需要做channels 的转换
        for (int i = 0; i < otherChannelList.get(0).size(); i++) {
            String channelName = otherChannelList.get(0).get(i).getChannelName();
            Channel channel1 = otherChannelList.get(0).get(i);
            com.company.news.bean.Channel channel = new com.company.news.bean.Channel(channel1.getChannelName(), i + "");
            mSaveRecommendChannelList1.add(channel);
            String title = mSaveRecommendChannelList1.get(i).title;
            String channelCode = mSaveRecommendChannelList1.get(i).channelCode;
//            Log.e("ChannelSelectedActivity", "==未==-选中的--==频道名字==" + channelName + "=====code=====" + channelCode);
        }
//        Log.e("ChannelSelectedActivity", "==未==-选中的--=转换后的list===" + mSaveRecommendChannelList1.size());
//        Log.e("ChannelSelectedActivity", "====-选中的--=转换后的list===" + mSaveChannelList.size());
//        Log.e("ChannelSelectedActivity", "====-选中的--====" + channelList.size());
//        Log.e("ChannelSelectedActivity", "==未==-选中的--====" + otherChannelList.get(0).size());
    }

    @Override
    public void channelEditStart() {

    }


}
